//
//  HLEToastView.m
//  HLETips
//
//  Created by hle1 on 2020/5/21.
//

#import "HLEToastView.h"
#import "HLEToastContentView.h"
#import "HLEToastHelper.h"
#import <Masonry/Masonry.h>

static NSMutableArray <HLEToastView *> *kToastViews = nil;

@interface HLEToastView()
@property(nonatomic, strong) UIView *parentView;
@property(nonatomic, strong) UIView *backgroundView;
@property(nonatomic, strong) HLEToastContentView *contentView;
@property(nonatomic, strong) UIView *maskView;
@property(nonatomic, weak) NSTimer *hideDelayTimer;
@property(nonatomic, strong) UIView *contentCustomView;
@end

@implementation HLEToastView

+ (HLEToastView *)showLoading:(nullable NSString *)text
        inView:(UIView *)view
hideAfterDelay:(NSTimeInterval)delay
{
    HLEToastView *toastView = [self createTipsToView:view];
    [toastView showLoading:text inView:view hideAfterDelay:delay];
    return toastView;
}

+ (HLEToastView *)showSucceed:(NSString *)text
        inView:(UIView *)view
               hideAfterDelay:(NSTimeInterval)delay
{
    HLEToastView *toastView = [self createTipsToView:view];
    [toastView showSucceed:text  inView:view hideAfterDelay:delay];
    return toastView;
}

+ (HLEToastView *)showError:(nullable NSString *)text
        inView:(UIView *)view
hideAfterDelay:(NSTimeInterval)delay
{
    HLEToastView *toastView = [self createTipsToView:view];
    [toastView showError:text inView:view hideAfterDelay:delay];
    return toastView;
}

+ (HLEToastView *)createTipsToView:(UIView *)view {
    HLEToastView *tips = [[HLEToastView alloc] initWithView:view];
    [view addSubview:tips];
    return tips;
}

- (void)showLoading:(nullable NSString *)text
        inView:(UIView *)view
hideAfterDelay:(NSTimeInterval)delay
{
    UIActivityIndicatorView *testActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [testActivityIndicator startAnimating];
    self.contentCustomView = testActivityIndicator;
    self.contentView.customView = self.contentCustomView;
    [self.contentView initSubviews];
    self.contentView.textLabel.text = text ?: @"";
    self.contentView.textLabel.textColor = UIColor.whiteColor;
    if (delay != 0) {
        [self hideAnimated:YES afterDelay:delay];
    }
}


- (void)showSucceed:(NSString *)text inView:(UIView *)view
       hideAfterDelay:(NSTimeInterval)delay
{
    self.contentCustomView = [[UIImageView alloc] initWithImage:[[HLEToastHelper imageWithName:@"deleteSuccess"] imageWithRenderingMode:UIImageRenderingModeAutomatic]];
    self.contentView.customView = self.contentCustomView;
    [self.contentView initSubviews];
    self.contentView.textLabel.text = text ?: @"";
    self.contentView.textLabel.textColor = UIColor.whiteColor;
    if (delay != 0) {
        [self hideAnimated:YES afterDelay:delay];
    }
}

- (void)showError:(nullable NSString *)text
        inView:(UIView *)view
hideAfterDelay:(NSTimeInterval)delay
{
    self.contentCustomView = [[UIImageView alloc] initWithImage:[[HLEToastHelper imageWithName:@"deleteFail"] imageWithRenderingMode:UIImageRenderingModeAutomatic]];
    self.contentView.customView = self.contentCustomView;
    [self.contentView initSubviews];
    self.contentView.textLabel.text = text ?: @"";
    self.contentView.textLabel.textColor = UIColor.whiteColor;
    if (delay != 0) {
        [self hideAnimated:YES afterDelay:delay];
    }
}

- (nonnull instancetype)initWithView:(nonnull UIView *)view {
    NSAssert(view, @"view不能为空");
    if (self = [super initWithFrame:view.bounds]) {
        _parentView = view;
        [self didInitialize];
    }
    return self;
}
- (void)didInitialize {
    
    self.contentView = [[HLEToastContentView alloc] init];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    
    _maskView = [[UIView alloc] init];
    self.maskView.backgroundColor = UIColor.clearColor;
    [self addSubview:self.maskView];
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.edges.mas_equalTo(0);
    }];
}

- (void)hideAnimated:(BOOL)animated afterDelay:(NSTimeInterval)delay
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:delay target:self selector:@selector(handleHideTimer:) userInfo:@(animated) repeats:NO];
      [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
      self.hideDelayTimer = timer;
}

- (void)handleHideTimer:(NSTimer *)timer {
    [self hideAnimated:[timer.userInfo boolValue]];
}

- (void)hideAnimated:(BOOL)animated {
    self.backgroundView.alpha = 0.0;
    self.contentView.alpha = 0.0;
    [self didHideWithAnimated:animated];
}

- (void)didHideWithAnimated:(BOOL)animated {
    [self.hideDelayTimer invalidate];
    self.alpha = 0.0;
    [self removeFromSuperview];
}

- (void)didMoveToSuperview {
    if (!kToastViews) {
        kToastViews = [[NSMutableArray alloc] init];
    }
    if (self.superview) {
        // show
        if (![kToastViews containsObject:self]) {
            [kToastViews addObject:self];
        }
    } else {
        // hide
        if ([kToastViews containsObject:self]) {
            [kToastViews removeObject:self];
        }
    }
}

+ (void)hideAllTipsInView:(UIView *)view {
    [self hideAllToastInView:view animated:NO];
}

+ (void)hideAllTips
{
    [self hideAllToastInView:nil animated:NO];
}

+ (BOOL)hideAllToastInView:(UIView *)view animated:(BOOL)animated {
    NSArray *toastViews = [self allToastInView:view];
    BOOL result = NO;
    for (HLEToastView *toastView in toastViews) {
        result = YES;
        [toastView hideAnimated:animated];
    }
    return result;
}

+ (nullable NSArray <HLEToastView *> *)allToastInView:(UIView *)view {
    if (!view) {
        return kToastViews.count > 0 ? [kToastViews mutableCopy] : nil;
    }
    NSMutableArray *toastViews = [[NSMutableArray alloc] init];
    for (UIView *toastView in kToastViews) {
        if (toastView.superview == view && [toastView isKindOfClass:self]) {
            [toastViews addObject:toastView];
        }
    }
    return toastViews.count > 0 ? [toastViews mutableCopy] : nil;
}

- (void)dealloc {
    if ([kToastViews containsObject:self]) {
        [kToastViews removeObject:self];
    }
}

@end

